package org.ting.j20250110_gobang.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class OnlineUserManager {
    //使用ConcurrentHashMap保证线程安全
    //维护大厅中在线用户
    private Map<Integer, WebSocketSession> onlineUser = new ConcurrentHashMap<>();
    //维护游戏房间中的用户
    private Map<Integer, WebSocketSession> inRoomUser = new ConcurrentHashMap<>();
    public void enterGameRoom(int userId, WebSocketSession session) {
        //用户进入游戏房间
        inRoomUser.put(userId, session);
    }
    public void exitGameRoom(int userId) {
        //用户退出游戏房间
        inRoomUser.remove(userId);
    }
    public WebSocketSession getFromRoom(int userId) {
        //获取用户的websocket会话
        return inRoomUser.get(userId);
    }
    public void enterGameHall(int userId, WebSocketSession session) {
        //用户上线
        onlineUser.put(userId, session);
    }
    public void exitGameHall(int userId) {
        //用户下线
        onlineUser.remove(userId);
    }

    public WebSocketSession getFromHall(int userId) {
        //获取用户的websocket会话
        return onlineUser.get(userId);
    }
}
